﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OperaHouse.Business.News;
using OperaHouse.Configuration;
using Telerik.Web.Mvc;
using OperaHouse.Web.Areas.Admin.Models;
using OperaHouse.Domain.News;

namespace OperaHouse.Web.Areas.Admin.Controllers
{
    [Authorize]
    public class NewsController : BaseController
    {
        #region Fields
        private readonly INewsService _newsService;
        private readonly int newsPagesize;
        #endregion

        #region Constructors
        public NewsController(INewsService newsService)
        {
            this._newsService = newsService;
            this.newsPagesize = GeneralSetting.AdminPageSize;
        }
        #endregion

        public ActionResult Index()
        {
            return RedirectToAction("List");
        }

        public ActionResult List()
        {
            var news = _newsService.GetAllNews(null, 0, newsPagesize);
            var gridModel = new GridModel<NewsItemModel>
            {
                Data = news.Select(x =>
                {
                    NewsItemModel m = new NewsItemModel();
                    m.Id = x.Id;
                    m.Title = x.Title;
                    m.Published = x.Published;
                    m.CreatedOn = x.CreatedOn;
                    return m;
                }),
                Total = news.TotalCount
            };
            return View(gridModel);
        }

        [HttpPost, GridAction(EnableCustomBinding = true)]
        public ActionResult List(GridCommand command)
        {
            var news = _newsService.GetAllNews(null, command.Page - 1, command.PageSize);
            var gridModel = new GridModel<NewsItemModel>
            {
                Data = news.Select(x =>
                {
                    NewsItemModel m = new NewsItemModel();
                    m.Id = x.Id;
                    m.Title = x.Title;
                    m.Published = x.Published;
                    m.CreatedOn = x.CreatedOn;
                    return m;
                }),
                Total = news.TotalCount
            };
            return new JsonResult
            {
                Data = gridModel
            };
        }

        public ActionResult Create()
        {
            var model = new NewsItemModel();
            //default values
            model.Published = true;
            return View(model);
        }

        [HttpPost, ValidateInput(false)]
        public ActionResult Create(NewsItemModel model)
        {
            if (ModelState.IsValid)
            {
                NewsItem newsItem = NewsItemModel.Create(model);
                newsItem.CreatedOn = DateTime.Now;
                _newsService.InsertNews(newsItem);
                return RedirectToAction("List");
            }
            return View(model);
        }

        public ActionResult Edit(int id)
        {
            var newsItem = _newsService.GetNewsById(id);
            if (newsItem == null)
                //No news item found with the specified id
                return RedirectToAction("List");

            NewsItemModel model = new NewsItemModel();
            model.Id = newsItem.Id;
            model.Title = newsItem.Title;
            model.Short = newsItem.Short;
            model.Full = newsItem.Full;
            model.Published = newsItem.Published;
            model.CreatedOn = newsItem.CreatedOn;
            return View(model);
        }

        [HttpPost, ValidateInput(false)]
        public ActionResult Edit(NewsItemModel model)
        {
            var newsItem = _newsService.GetNewsById(model.Id);
            if (newsItem == null)
                //No news item found with the specified id
                return RedirectToAction("List");

            if (ModelState.IsValid)
            {
                newsItem.Title = model.Title;
                newsItem.Short = model.Short;
                newsItem.Full = model.Full;
                newsItem.Published = model.Published;
                _newsService.UpdateNews(newsItem);
                return RedirectToAction("List");
            }

            return View(model);
        }

        [HttpPost]
        public ActionResult Delete(int id)
        {
            var newsItem = _newsService.GetNewsById(id);
            if (newsItem == null)
                return Json(new
                {
                    result = "fail"
                });

            _newsService.DeleteNews(newsItem);

            return Json(new
            {
                result = "ok"
            });
        }
    }
}
